<?
require("config.php");

if ($method=='product') {
	$where="where b.id=$search";
	$title="Transaction records of product :<br> ".$rec->name."<br>".$rec->brand;
}

if ($method=='person') {
	$where=" where t.person='$search'";
	$title=$search."'s transaction records";
}

$sql="select t.id,t.pid,t.trans_time,t.person,t.amount,t.orderno,
		b.name,b.brand,t.cancelled,b.code
	 from pro_basic as b left outer join pro_transaction as t on b.id=t.pid
	  $where  order by t.id asc";

$r=myQuery($sql);



header("Content-type:application/vnd.ms-excel");  
header("Content-Disposition:filename=".$r[0][9]."_transaction_". date("Ymd") . ".xls");  


echo $r[0][9]."\t\n";

echo $r[0][6]."\t\n";
echo $r[0][7]."\t\n\t\n\t\n";


echo "Transaction Date\t";  
echo "Order No.\t";
echo "Order Qty\t";
echo "Re-stock Qty\t"; 
echo "Customer\t"; 
echo "Customer email\t";
echo "Order Status\t";
echo "Shipping Status\t";
echo "Delivery Date\t\n";
for ($i=0;$i<count($r);$i++){

if ($r[$i][2]>strtotime('1980-01-01')) $ttime=date('Y-m-d H:i:s',$r[$i][2]);
else $ttime='';


		if (is_numeric($r[$i][3])) {
			$sql="select firstname,lastname,email from cus_info where id=".$r[$i][3];
			$p=myQuery($sql);
			$person=$p[0][0].' '.$p[0][1].' ['.str_pad($r[$i][3],5,'0',STR_PAD_LEFT).']';
			$restock='0';
			$purchase=abs($r[$i][4]);
		}
		else {
			$person=$r[$i][3];
			$restock=$r[$i][4];
			$purchase='0';
			$sql="select id,username,email from user_profile where name='".$r[$i][3]."'";
			$p=myQuery($sql);
		}		
	
		if ($r[$i][5]) {
			$orderno=str_pad($r[$i][5],5,'0',STR_PAD_LEFT);
			$sql="select shipping_status,delivery_date from cus_order where no=".$r[$i][5];
			$o=myQuery($sql);
			$shipping_status=$o[0][0];
			if ($o[0][1]>strtotime('1980-01-01')) $delivery_date=date('Y-m-d',$o[0][1]);
				else $delivery_date='';			
		}
		else {
			$orderno='';
			$shipping_status='';
			$delivery_date='';
		}

echo $ttime . "\t"; 
echo $orderno . "\t"; 
echo $purchase . "\t"; 
echo $restock . "\t"; 
echo $person . "\t"; 
echo $p[0][2] . "\t"; 
echo ($r[$i][8]?'Cancelled':'')."\t";
echo $shipping_status . "\t"; 
echo $delivery_date . "\t\n"; 
}

echo "\n"; 



?>